package org.acme.cluster.controller;

import org.acme.cluster.po.SysSms;
import org.acme.cluster.service.SysSmsService;
import org.jgroups.util.UUID;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SysSmsController {

    @Resource
    private SysSmsService sysSmsService;

    /****
     * 发送短信
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "sendSms")
    public SysSms sendSms() throws Exception {
        SysSms sysSms = new SysSms();
        sysSms.setPhone("13800138000");
        sysSms.setContent(UUID.randomUUID().toString());
        sysSmsService.addSms(sysSms);
        return sysSms;
    }

    /****
     * 批量发送短信
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "sendSmsBatch")
    public int sendSmsBatch(@RequestParam(defaultValue = "80") int size) throws Exception {
        List<SysSms> sysSmsList = new ArrayList<SysSms>();
        for (int i = 0; i < size; i++) {
            SysSms sysSms = new SysSms();
            sysSms.setPhone("13800138000");
            sysSms.setContent(UUID.randomUUID().toString());
            sysSmsList.add(sysSms);
        }
        sysSmsService.addSmsBatch(sysSmsList);
        return sysSmsList.size();
    }
}
